package com.hna.eking.Controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hna.eking.Exception.RunTimeException;
//import com.hna.eking.SparkUtils.SparkRun;
//import com.hna.eking.SparkUtils.SparkRunHive;

import java.util.UUID;

@RestController
public class FileAnyCtl {
	/*
	 * {\"TableName\":\"\",\"RetList\":\"张三\"}"
	 * Parm={{"TableName":"SuperMall.txt"},{"RetList":[{"Predicate":"count",
	 * "RetItem":"aaa"},{"Predicate":"sum","RetItem":"bbbb"}]},{"GrpList":[{
	 * "GrpName":"ccc"},{"GrpName":"dd"}]}}
	 */
	@RequestMapping(value = "/FileCtl.htm", method = RequestMethod.GET)
	public String FileCtlGet(@RequestParam(value = "Parm") String Parm) {

		logger.debug("parm = " + Parm);
		JSONObject json = JSONObject.parseObject(Parm);
		logger.debug("parm = " + json.toJSONString());
		/* get table name from request */
		String TableName = json.getString("TableName");
		logger.debug("TableName:" + TableName);
		/* get return list include predicate from request */
		JSONArray RetArry = json.getJSONArray("RetList");
		String RetList = " ";// first space here
		for (int i = 0; i < RetArry.size(); i++) {
			JSONObject RetItem = RetArry.getJSONObject(i);
			String RetStr = String.format("%s(%s)", RetItem.getString("Predicate"), RetItem.getString("RetItem"));
			logger.debug("Ret List:" + RetStr);

			if (RetArry.size() == i + 1) {
				RetList = RetList + RetStr + " ";
				continue;
			}
			RetList = RetList + RetStr + ",";
		}

		/* get group list from request */
		JSONArray GrpArry = json.getJSONArray("GrpList");
		String grpList = " ";// first space here
		for (int i = 0; i < GrpArry.size(); i++) {
			JSONObject GrpItem = GrpArry.getJSONObject(i);
			String GrpStr = GrpItem.getString("GrpName");
			logger.debug("Group List:" + GrpStr);

			if (GrpArry.size() == i + 1) {
				grpList = grpList + GrpStr + " ";
				continue;
			}
			grpList = grpList + GrpStr + ",";
		}

		/* joint sql string from request string */
		String SqlStr = String.format("select %s from %s where 1 = 1 group by %s", RetList, TableName, grpList);
		logger.debug("sql string is:" + SqlStr);
		/* get result from file with spark sql */

		/* joint result json */
		JSONObject resultObj = new JSONObject();
//		SparkRunHive hivectx = new SparkRunHive();
//		hivectx.RunSql(SqlStr);
		return SqlStr;

	}

	@RequestMapping(value = "/FileCtl", method = RequestMethod.PUT)
	public String FileCtlPut(HttpRequest req) {
		String test  = req.getMethodValue();
		
//		SparkRunHive hivectx = new SparkRunHive();
//		hivectx.RunSql("");
		return null;
		
//		try {
//			SparkRun ctx = new SparkRun("BigDataTest-0.1.1.jar", "com.hna.eking.main.TestMain", "yarn");
//			ctx.MainSessionID = "11122233444";
//			ctx.ResultUUID = UUID.randomUUID().toString();
//
//			String ret = ctx.LachClass("xxxxxxx");
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (RunTimeException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	private static final Logger logger = LoggerFactory.getLogger(FileAnyCtl.class);
}
